package cn.stylefeng.guns.modular.fi.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.fi.entity.FiFreightDetails;
import cn.stylefeng.guns.modular.fi.entity.FiFreightDetailsLog;
import cn.stylefeng.guns.modular.fi.model.params.FiFreightDetailsParam;
import cn.stylefeng.guns.modular.fi.model.result.FiFreightDetailsResult;
import cn.stylefeng.guns.modular.fi.service.FiFreightDetailsLogService;
import cn.stylefeng.guns.modular.fi.service.FiFreightDetailsService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 控制器
 *
 * @author zhangzh
 * @Date 2024-11-04 11:08:38
 */
@Controller
@RequestMapping("/fiFreightDetails")
public class FiFreightDetailsController extends BaseController {

    private String PREFIX = "/rec/carrierRecFeeCalc";

    @Autowired
    private FiFreightDetailsService fiFreightDetailsService;

    @Autowired
    private FiFreightDetailsLogService fiFreightDetailsLogService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/fiFreightDetails.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/fiFreightDetails_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/carrierRecFeeCalc_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FiFreightDetailsParam fiFreightDetailsParam) {
        this.fiFreightDetailsService.add(fiFreightDetailsParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FiFreightDetailsParam fiFreightDetailsParam) {
        this.fiFreightDetailsService.update(fiFreightDetailsParam);
        return ResponseData.success();
    }


    @RequestMapping("/updateCalcCost")
    @ResponseBody
    public ResponseData updateCalcCost(FiFreightDetailsParam fiFreightDetailsParam) {
        this.fiFreightDetailsService.updateCalcCost(fiFreightDetailsParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FiFreightDetailsParam fiFreightDetailsParam) {
        this.fiFreightDetailsService.delete(fiFreightDetailsParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FiFreightDetailsParam fiFreightDetailsParam) {
        FiFreightDetails detail = this.fiFreightDetailsService.getById(fiFreightDetailsParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FiFreightDetailsParam fiFreightDetailsParam,String condition ) {
        User user = userService.getcurrentUser();
        if(ToolUtil.isNotEmpty(user.getOperateId()) && ToolUtil.isEmpty(fiFreightDetailsParam.getOperateId())){
            fiFreightDetailsParam.setOperateId(user.getOperateId());
        }
        fiFreightDetailsParam.setDeptId(user.getDeptId());
        return this.fiFreightDetailsService.findPageBySpec(fiFreightDetailsParam,condition);
    }


    @ResponseBody
    @RequestMapping("/listAll")
    public List<FiFreightDetailsResult> listAll(FiFreightDetailsParam fiFreightDetailsParam, String condition ) {
        User user = userService.getcurrentUser();
        if(ToolUtil.isNotEmpty(user.getOperateId()) && ToolUtil.isEmpty(fiFreightDetailsParam.getOperateId())){
            fiFreightDetailsParam.setOperateId(user.getOperateId());
        }
        fiFreightDetailsParam.setDeptId(user.getDeptId());
        return this.fiFreightDetailsService.listAll(fiFreightDetailsParam,condition);
    }

    /**
     * 审核
     * @param ids
     * @return
     */
    @RequestMapping("/shenhe")
    @ResponseBody
    public ResponseData shenhe(@RequestParam("ids") String ids) {
        return this.fiFreightDetailsService.shenhe(ids);
    }

    /**
     * 推送
     * @param ids
     * @return
     */
    @RequestMapping("/tuisong")
    @ResponseBody
    public ResponseData tuisong(@RequestParam("ids") String ids ,@RequestParam("carrierUserCode") String carrierUserCode) {
        return this.fiFreightDetailsService.tuisong(ids,carrierUserCode);
    }

    // 时序图页面
    @RequestMapping("/tooTimeLine")
    public String tooTimeLine(Long detailsId) {
        return PREFIX + "/tooTimeLine.html";
    }

    @RequestMapping("/tooTimeLineDetailed")
    @ResponseBody
    public ResponseData tooTimeLineWeb(Long detailsId){
        List<FiFreightDetailsLog> list = fiFreightDetailsLogService.lambdaQuery()
                .eq(FiFreightDetailsLog::getDetailsId, detailsId)
                .orderByAsc(FiFreightDetailsLog::getOpTime)
                .list();
        return ResponseData.success(list);
    }

}


